
import Pure from 'pure-frontend';
import { globalUtils } from '../common/js/global-utils/global-utils-all';
import { CurrentRunningApp } from '../common/js/current-running-app';
import { uiUtils } from '../common/js/ui-utils/ui-utils-all';

/* SOURCE-CODE-START */

"use strict";

$(function () {

  Pure.fn.Ajax.callService('api/account/user/getCurrentUser')
    .thenResult(function (userResult) {
      if (!uiUtils.common.checkWebResultSuccess(userResult)) {
        return;
      }

      var user = userResult.data;
      $('.home-top-bar-account>a').text(user.userName);
      CurrentRunningApp.addData(CurrentRunningApp.DATA_PRIVILEGES, user.privileges);
      CurrentRunningApp.addData(CurrentRunningApp.DATA_TENANT_ID, user.tenantId);
      CurrentRunningApp.addData(CurrentRunningApp.DATA_USER_ID, user.userId);
      CurrentRunningApp.addData(CurrentRunningApp.DATA_USER_NAME, user.userName);
      CurrentRunningApp.addData(CurrentRunningApp.DATA_USER_TYPE, user.userType);

      // 获取菜单列表后渲染菜单栏
      Pure.fn.Ajax.callService('api/menu/menu/listHomeMenus')
        .thenResult(function (result) {
          if (!uiUtils.common.checkWebResultSuccess(result)) {
            return;
          }

          var menuArray = result.data;
          // 生成菜单栏 HTML
          var menuHtml = createHomeMenuBar(menuArray);
          // 渲染菜单栏
          renderHomeMenuBar(menuHtml, function (url) {
            Pure.app.loadView(url);
          });

          // 加载页面
          var url = getCurrentOrDefaultUrl();
          if (globalUtils.isNotEmptyString(url)) {
            Pure.app.loadView(url);
          }

          // 展开对应的菜单
          var menuLinkSelector = globalUtils.formatString('.home-menu-bar a[data-menu-url="{0}"]', url);
          var jqMenuLink = $(menuLinkSelector);
          var jqMenuChildren = jqMenuLink.parents('.home-menu-children');
          jqMenuChildren.toggle(1, function () {
            var jqMenuItem = jqMenuChildren.parents('.home-menu-dropdown-item');
            jqMenuItem.addClass('home-menu-item-expand');
          });
        });
    });

  /**
   * 获取当前或默认 URL
   * @returns {string}
   */
  function getCurrentOrDefaultUrl() {
    var url = location.href;
    var index = url.indexOf('#');

    if (index >= 0) {
      url = url.substring(index + 1);

      if (url.startsWith('/')) {
        url = url.substring(1);
      }
    } else {
      /*url = $('.home-menu-item-default > a').data('menu-url');

      if (globalUtils.isNullOrUndefined(url)) {
        url = 'default';
      }*/
      url = 'default';
    }

    return url;
  }

  var jqWindow = $(window);
  var jqBody = $('body');
  var jqLeft = $('.home-left-side');
  var jqRight = $('.home-right-side');

  var jqHomeMenuFold = $('.home-menu-fold');
  var jqHomeMenuUnfold = $('.home-menu-unfold');
  var jqHomeMenuBackdrop = $('.home-menu-backdrop');
  var homeFoldModeClass = 'home-fold-mode';
  var homeUnfoldModeClass = 'home-unfold-mode';

  // 点击折叠菜单时调用
  jqHomeMenuFold.on('click', function () {
    jqLeft.css('left', '-202px');
    jqRight.css('left', '0');

    jqBody.removeClass(homeUnfoldModeClass)
      .addClass(homeFoldModeClass);
  });

  // 点击展开菜单时调用
  jqHomeMenuUnfold.on('click', function () {
    jqLeft.css('left', '0');
    jqRight.css('left', '202px');

    jqBody.removeClass(homeFoldModeClass)
      .addClass(homeUnfoldModeClass);
  });

  jqHomeMenuBackdrop.on('click', function () {
    jqHomeMenuFold.trigger('click');
  });

  // 窗口大小改变时调用
  jqWindow.on('resize', function () {
    var width = jqWindow.width();

    if (width >= 768) {
      if (jqBody.hasClass(homeUnfoldModeClass)) {
        jqBody.removeClass(homeUnfoldModeClass);
        jqLeft.css('left', '');
        jqRight.css('left', '');
      }
    } else {
      if (jqBody.hasClass(homeFoldModeClass)) {
        jqBody.removeClass(homeFoldModeClass);
        jqLeft.css('left', '');
        jqRight.css('left', '');
      }
    }
  });

});

/* SOURCE-CODE-END */
